package com.bw.common.widget

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.util.Log
import android.view.KeyEvent
import com.tencent.smtt.export.external.interfaces.ConsoleMessage
import com.tencent.smtt.export.external.interfaces.JsPromptResult
import com.tencent.smtt.export.external.interfaces.JsResult
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

/**
 * X5浏览器
 */
class X5WebView constructor(context: Context,attrs:AttributeSet):WebView(context,attrs) {

    init {
        //设置客户端
        this.webViewClient = X5WebViewClient()
        this.webChromeClient = X5WebChromeClient()
        //初始化参数
        initX5WebView()
    }

    private fun initX5WebView() {
        var webString = this.settings
        webString.javaScriptEnabled = true//是否支持JS脚本
        webString.allowFileAccess = true//是否支持文件访问
        webString.loadsImagesAutomatically = true //是否支持图片自动加载
        webString.javaScriptCanOpenWindowsAutomatically = true//是否支持JS弹窗
        webString.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webString.setSupportMultipleWindows(false)//是否支持多窗口
        webString.useWideViewPort = true//是否支持小窗口
        webString.setSupportZoom(false)//是否支持页面缩放
        webString.builtInZoomControls = false//是否支持放大缩小控制按钮
        webString.setAppCacheEnabled(true)//是否支持缓存
        webString.cacheMode = WebSettings.LOAD_NO_CACHE// 缓存模式
        webString.domStorageEnabled = true//是否支持Dom存储
        webString.setGeolocationEnabled(false)// 是否支持定位



    }

    /**
     * 控制H5页面加载
     */
    inner class X5WebViewClient: WebViewClient(){
        //H5页面加载完成
        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
        }
        //H5页面关闭
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
        }
        //拦截H5页面中的按键点击事件
        override fun shouldOverrideKeyEvent(view: WebView?, event: KeyEvent?): Boolean {
            return super.shouldOverrideKeyEvent(view, event)
        }
        //控制H5页面Url加载
        override fun shouldOverrideUrlLoading(
            webView: WebView?,
            url: String?
        ): Boolean {
            webView?.loadUrl(url) //可以替换的Url

            return true
        }
    }
    /**
     * 控制H5页面中所有用户交互
     */
    inner class X5WebChromeClient: WebChromeClient(){
        override fun onJsAlert(
            view: WebView?,
            url: String?,
            message: String?,
            result: JsResult?
        ): Boolean {
            return true
        }

        override fun onJsConfirm(
            view: WebView?,
            url: String?,
            message: String?,
            result: JsResult?
        ): Boolean {
            return true
        }

        override fun onJsPrompt(
            view: WebView?,
            url: String?,
            message: String?,
            defaultValue: String?,
            result: JsPromptResult?
        ): Boolean {
            return true
        }

        override fun onJsBeforeUnload(
            view: WebView?,
            url: String?,
            message: String?,
            result: JsResult?
        ): Boolean {
            return true
        }

        override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
            Log.i("X5WebChromeClient", "onConsoleMessage: ${consoleMessage?.message()}")
            return true
        }

    }



}